package com.example.myblog01.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//为拦截做准备
public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        用户为空就重定向到登录页
        if(request.getSession().getAttribute("user") == null){
//            禁止访问
            response.sendRedirect("/admin");
            return false;
        }

        return true;
    }
}
